package com.mail.mailservice.controller;
import cn.hutool.core.io.resource.ResourceUtil;
import com.mail.mailservice.service.MailService;
import com.mail.mailservice.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import java.net.URL;

@Controller
public class EmailController {

    @Autowired
    private MailService mailService;

    @Autowired
    private TemplateEngine templateEngine;

    /**
     * 测试发送简单邮件
     */
    @GetMapping("/testSendSimpleMail")
    @ResponseBody
    public ResultData sendSimpleMail() {
        ResultData result = new ResultData();
        try {
            mailService.sendSimpleMail("接收方邮箱@qq.com", "这是一封简单邮件", "这是一封普通的SpringBoot测试邮件");
            result.setMsg("发送成功");
            result.setStatus(200);
        }catch (Exception ex){
            result.setMsg("失败"+ex.getMessage());
            result.setStatus(0);
        }
        return result;
    }



    /**
     * 发送html邮件
     * @return
     * @throws MessagingException
     */
    @GetMapping("/sendHtmlMail")
    @ResponseBody
    public ResultData sendHtmlMail() throws MessagingException {
        ResultData result = new ResultData();
        Context context = null;
        try {
            context = new Context();
            context.setVariable("project", "Spring Boot email");
            context.setVariable("author", "picacho");
            context.setVariable("url", "www.picacho.top");

            String emailTemplate = templateEngine.process("welcome", context);
            mailService.sendHtmlMail("接收方邮箱@qq.com", "这是一封模板HTML邮件", emailTemplate);
        }catch (Exception ex){
            result.setMsg("失败"+ex.getMessage());
            result.setStatus(0);
        }
        return result;
    }


    /**
     * 发送带附件的邮件
     * @throws MessagingException
     */
    @GetMapping("/sendAttachmentsMail")
    @ResponseBody
    public ResultData sendAttachmentsMail() throws MessagingException {
        ResultData result = new ResultData();
        try {
            URL resource = ResourceUtil.getResource("static/1.png");
            System.out.println(resource.getPath());
            mailService.sendAttachmentsMail("接收方邮箱@qq.com", "这是一封带附件的邮件", "邮件中有附件，请注意查收！", resource.getPath());
        }catch (Exception ex){
            result.setMsg("失败"+ex.getMessage());
            result.setStatus(0);
        }
        return result;

    }



//    /**
//     * 发送自定义模版的html邮件
//     * @return
//     * @throws MessagingException
//     */
//    @GetMapping("/sendHtmlMail2")
//    @ResponseBody
//    public ResultData sendHtmlMail2() throws MessagingException {
//        ResultData result = new ResultData();
//        try {
//            SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
//            ApplicationContext context = new ApplicationContext();
//            templateResolver.setApplicationContext(context);
//            templateResolver.setCacheable(false);
//            templateResolver.setPrefix("classpath:/email/");
//            templateResolver.setSuffix(".html");
//            templateEngine.setTemplateResolver(templateResolver);
//
//            Context context = new Context();
//            context.setVariable("project", "Spring Boot email");
//            context.setVariable("author", "picacho");
//            context.setVariable("url", "www.picacho.top");
//
//            String emailTemplate = templateEngine.process("index", context);
//            mailService.sendHtmlMail("接收方邮箱@qq.com", "这是一封模板HTML邮件", emailTemplate);
//        }catch (Exception ex){
//            result.setMsg("失败"+ex.getMessage());
//            result.setStatus(0);
//        }
//        return result;
//
//    }


    /**
     * 发送静态资源
     * @return
     * @throws MessagingException
     */
    @GetMapping("/sendResourceMail")
    @ResponseBody
    public ResultData sendResourceMail() throws MessagingException {

        ResultData result = new ResultData();
        try {
            String rscId = "picacho";
            String content = "<html><body>这是带静态资源的邮件<br/><img src=\'cid:" + rscId + "\' ></body></html>";
            URL resource = ResourceUtil.getResource("static/1.png");
            mailService.sendResourceMail("接收方邮箱6@qq.com", "这是一封带静态资源的邮件", content, resource.getPath(), rscId);
        }catch (Exception ex){
            result.setMsg("失败"+ex.getMessage());
            result.setStatus(0);
        }
        return result;

    }







}
